package com.imelite.qyh.action;

import com.imelite.qyh.data.JxcpcsAccessBean;
import com.imelite.qyh.data.JxcpcsAccessPageBean;
import com.imelite.qyh.data.util.Jxcpcs_Util;
import com.imelite.qyh.model.*;
import com.imelite.qyh.service.*;
import com.imelite.qyh.util.BaseStringUtil;
import com.imelite.qyh.util.BaseUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class JxcpcsAction {
    @Autowired
    private JxcpcsService jxcpcsService;
    @Autowired
    private JxcpcsfzService jxcpcsfzService;
    @Autowired
    private JxcpymrzService jxcpymrzService;
    @Autowired
    private YycpbgService yycpbgService;
    @Autowired
    private EliteCRMView_YyxxService eliteCRMView_yyxxService;
    @Autowired
    private JxcpkcjsService jxcpkcjsService;

    @RequestMapping("/com/imelite/qyh/JxcpcsAction/getTestPage")
    public String getTestPage(HttpServletRequest request, HttpServletResponse response,String yyxxid,String iddep){
        if(BaseStringUtil.isBlank(iddep)){
            return "/Elite/jxcpLogin";
        }
        request.setAttribute("iddep",iddep);
        request.setAttribute("yyxxid",yyxxid);
        return "/Elite/yyxx/test1";
//        return "/Elite/JxcpcsPage";
    }

    @RequestMapping("/com/imelite/qyh/JxcpcsAction/getTest2")
    public String getTest2(HttpServletRequest request, HttpServletResponse response,String yyxxid,String iddep){
        if(BaseStringUtil.isBlank(iddep)){
            return "/Elite/jxcpLogin";
        }
        List<Jxcpymrz> jxcpymrzList=jxcpymrzService.selectAll();

        int num=0;
        for(Jxcpymrz j:jxcpymrzList){
            num+=j.getCs().size();
        }
        request.setAttribute("num",num);
        request.setAttribute("iddep",iddep);
        request.setAttribute("yyxxid",yyxxid);
        return "/Elite/yyxx/test2";
    }

    @RequestMapping("/com/imelite/qyh/JxcpcsAction/getTest")
    public String getTest(HttpServletRequest request, HttpServletResponse response,String yyxxid,String iddep){
        if(BaseStringUtil.isBlank(iddep)){
            return "/Elite/jxcpLogin";
        }
        List<Jxcpymrz> jxcpymrzList=jxcpymrzService.selectAll();
        request.setAttribute("iddep",iddep);
        request.setAttribute("yyxxid",yyxxid);
        request.setAttribute("jxcpymrzList",jxcpymrzList);
        return "/Elite/JxcpcsPage";
    }

    @RequestMapping("/com/imelite/qyh/JxcpcsAction/getSubmitAccessPage")
    public String getSubmitAccessPage(HttpServletRequest request, HttpServletResponse response,String yyxxid,String iddep){
        Yycpbg yycpbg = yycpbgService.selectBYyxxid(yyxxid);
        EliteCRMView_Yyxx yyxx= eliteCRMView_yyxxService.selectById(yyxxid);
        List<JxcpkcjsWithBLOBs> jxcpkcjsList = jxcpkcjsService.selectAllByIddep(iddep);
        request.setAttribute("jxcpkcjsList",jxcpkcjsList);
        request.setAttribute("yyxx",yyxx);
        request.setAttribute("iddep",iddep);
        request.setAttribute("yycpbg",yycpbg);
        return "/Elite/yyxx/testAccess";
    }

    @RequestMapping("/com/imelite/qyh/JxcpcsAction/getAccessPage")
    public String getAccessPage(HttpServletRequest request, HttpServletResponse response,String yyxxid){
        Yycpbg yycpbg= yycpbgService.selectBYyxxid(yyxxid);
        EliteCRMView_Yyxx yyxx= eliteCRMView_yyxxService.selectById(yyxxid);
        String[] ids=yycpbg.getCpda().split(",");
        List<Jxcpcs> jxcpcsList=jxcpcsService.selectAll();
        List<Jxcpcs> khxz=new ArrayList<>();//客户选择的项
        String[] da= yycpbg.getCpda().split(",");
        for(Jxcpcs j:jxcpcsList){
            if(sfcz(da,j.getId())){
                khxz.add(j);
            }
        }
        List<Jxcpcsfz> jxcpcsfzList=jxcpcsfzService.selectAll();
        JxcpcsAccessPageBean jxcpcsAccessPageBean = Jxcpcs_Util.getBean(ids,jxcpcsfzList,jxcpcsList);


        if(BaseStringUtil.isNotBlank(yycpbg.getXqkc())){
            List<JxcpkcjsWithBLOBs> jxcpkcjsList=jxcpkcjsService.selectAllByIddep(yyxx.getIddep());
            List<JxcpkcjsWithBLOBs> jxcpkcTemp=new ArrayList<>();
            String[] xzkc=yycpbg.getXqkc().split("-");
            for(String s:xzkc){
                for(JxcpkcjsWithBLOBs j:jxcpkcjsList){
                    if(j.getId().equals(s)){
                        jxcpkcTemp.add(j);
                    }
                }
            }
            request.setAttribute("jxcpkcTemp",jxcpkcTemp);
        }

        request.setAttribute("yyxx",yyxx);
        request.setAttribute("yycpbg",yycpbg);
        request.setAttribute("khxz",khxz);
        request.setAttribute("jxcpcsAccessPageBean",jxcpcsAccessPageBean);
        return "/Elite/JxcpcsAccess";
    }

    @RequestMapping("/com/imelite/qyh/JxcpcsAction/getAccessPageToPrint")
    public String getAccessPageToPrint(HttpServletRequest request, HttpServletResponse response,String yyxxid){
        Yycpbg yycpbg= yycpbgService.selectBYyxxid(yyxxid);
        EliteCRMView_Yyxx yyxx= eliteCRMView_yyxxService.selectById(yyxxid);
        String[] ids=yycpbg.getCpda().split(",");
        List<Jxcpcs> jxcpcsList=jxcpcsService.selectAll();
        List<Jxcpcs> khxz=new ArrayList<>();//客户选择的项
        String[] da= yycpbg.getCpda().split(",");
        for(Jxcpcs j:jxcpcsList){
            if(sfcz(da,j.getId())){
                khxz.add(j);
            }
        }
        List<Jxcpcsfz> jxcpcsfzList=jxcpcsfzService.selectAll();
        JxcpcsAccessPageBean jxcpcsAccessPageBean = Jxcpcs_Util.getBean(ids,jxcpcsfzList,jxcpcsList);

        JxcpcsAccessBean one=getMax_one(jxcpcsAccessPageBean.getGNSK_list());
        JxcpcsAccessBean two=getMax_one(jxcpcsAccessPageBean.getSJTJ_list());
        request.setAttribute("yyxx",yyxx);
        request.setAttribute("yycpbg",yycpbg);
        request.setAttribute("one",one);
        request.setAttribute("two",two);
        request.setAttribute("khxz",khxz);
        request.setAttribute("jxcpcsAccessPageBean",jxcpcsAccessPageBean);
        return "/Elite/JxcpcsAccessPage";
    }

    private boolean sfcz(String[] da,String id){
        for(String s:da){
            if(s.equals(id)){
                return true;
            }
        }
        return false;
    }

    //获取客户属于 您的"感觉-思考-行动-观察"类型 中的哪一项，这个方法供页面使用
    private JxcpcsAccessBean getMax_one(List<JxcpcsAccessBean> jxcpcsAccessBeanList){
        JxcpcsAccessBean temp= null;
        for(JxcpcsAccessBean j:jxcpcsAccessBeanList){
            if(temp==null){
                temp=j;
            }else{
                if(j.getNum()>temp.getNum()){
                    temp=j;
                }
            }
        }
        return temp;
    }
}
